﻿using System.IO;
using System.Xml.Serialization;

namespace ArienToolkit.Serializer
{
    /// <summary>
    /// XML序列化帮助类
    /// </summary>
    public class XML : ISerializer
    {
        public bool Serialize<T>(object obj, string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));

                serializer.Serialize(stream, obj);

                stream.Close();
            }

            return true;
        }


        public T DeSerialize<T>(string filePath)
        {
            T obj;

            using (FileStream stream = new FileStream(filePath, FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                obj = (T)serializer.Deserialize(stream);
            }

            return obj;
        }
    }
}
